package com.ittx.dzt.tiktok.consumer.handler;

import cn.hutool.core.util.StrUtil;
import com.ittx.dzt.edi.constant.PlatformMessageConstant;
import com.ittx.dzt.edi.dto.EdiBatchSplitDto;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.handler.IPlatformApiHandler;
import com.ittx.dzt.edi.handler.PlatformApiHandlerFactory;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.SpringUtil;
import com.ittx.dzt.sys.util.json.JacksonTool;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class TiktokBatchSplitHandler {

    public void onMessage(String message, String logStr) {
        try {
            log.debug("tiktok[{}]拆分处理消费者入参:[{}]", logStr, message);
            EdiBatchSplitDto ediBatchSplitDto = JacksonTool.toEntity(message, EdiBatchSplitDto.class);
            PlatformApiParamDTO platformApiParam = ediBatchSplitDto.getPlatformApiParamDTO();
            platformApiParam.setPlatformIdentifierTag(ediBatchSplitDto.getPlatformIdentifierTag());
            //拆分后使用单号模式下载订单
            platformApiParam.setQueryType(PlatformApiConstant.ApiByTime.ID);
            String platformCode = platformApiParam.getStoreConfig().getPlatformCode();
            Class clazz = PlatformApiHandlerFactory.getInvokeHandler(platformCode, platformApiParam.getApi());
            if (clazz == null) {
                String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0001_MSG, platformApiParam.getRequestId(), platformCode, platformApiParam.getApi());
                log.error(msg);
            }
            IPlatformApiHandler platformApiHandler = (IPlatformApiHandler) SpringUtil.getBean(clazz);
            Result<?> result = platformApiHandler.process(platformApiParam);
            if (result.hasError()) {
                log.error("tiktok[{}]拆分后:[{}]拉取数据失败:[{}]", logStr, message, JacksonTool.toJson(result));
            }
        } catch (Exception e) {
            log.error("tiktok[{}]拆分处理消费者入参:[{}]", logStr, message);
            log.error("tiktok拆分数据请求异常", e);
        }

    }
}
